<template>
  <div id="content" v-loading="load">
    <el-page-header @back="goBack" content="搜索动态"> </el-page-header>
    <div class="header">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item>
          <el-input
            style="width: 250px"
            v-model="searchForm.keyword"
            placeholder="请输入要查询的关键字"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item style="margin-right: 5px">
          <el-button type="primary" @click="search" icon="el-icon-search"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="message-box">
      <div v-if="msgsList.length == 0">{{ tip }}</div>
      <template v-else>
        <div
          class="detail-content"
          v-for="(message, index) in msgsList"
          :key="message.mid"
        >
          <div class="detail">
            <div class="header">
              <div
                class="header-left"
                @click="getOtherUser(message.nickname, message.uid)"
              >
                <div class="avatar">
                  <el-avatar size="medium" :src="message.avatar"></el-avatar>
                </div>
                <div class="username_time">
                  <div class="username">{{ message.nickname }}</div>
                  <div class="time">
                    <i class="el-icon-pie-chart"></i>{{ message.date }}
                  </div>
                </div>
              </div>
              <div class="header-right">
                <el-button
                  type="danger"
                  size="mini"
                  @click="deleteMsg(message, index)"
                  v-if="userInfo.uid == message.uid"
                  >删帖</el-button
                >
              </div>
            </div>
            <div class="message-content" @click="toMessageDetail(message)">
              <h4>{{ message.title }}</h4>
              <p>{{ message.content }}</p>
              <div class="message-imgs" v-if="message.images.length != 0">
                <div class="imgsbox" v-if="message.images.length == 1">
                  <img
                    :src="message.images[0]"
                    style="
                      max-width: 353px;
                      max-height: 200px;
                      object-fit: cover;
                      border-radius: 5px;
                    "
                  />
                </div>
                <template v-if="message.images.length == 2">
                  <div class="imgsbox">
                    <el-image
                      v-for="(imgs, index) in message.images"
                      :key="index"
                      :src="imgs"
                      style="
                        width: 45%;
                        margin-right: 10px;
                        display: inline-block;
                      "
                    ></el-image>
                  </div>
                </template>
                <template v-if="message.images.length > 2">
                  <div class="imgsbox">
                    <el-image
                      v-for="(imgs, index) in message.images"
                      :key="index"
                      :src="imgs"
                      style="
                        width: 30%;
                        margin-right: 10px;
                        display: inline-block;
                      "
                    ></el-image>
                  </div>
                </template>
              </div>
            </div>
            <div class="footer">
              <div class="watched">
                <i class="el-icon-view" size="mini"></i>
                <span>浏览：{{ message.views }}</span>
              </div>
              <div class="comments" @click="toMessageDetail(message)">
                <i class="el-icon-chat-line-square" size="mini"></i>
                <span
                  >评论<i
                    style="font-style: normal"
                    v-if="message.comments != 0"
                    >:{{ message.comments }}</i
                  ></span
                >
              </div>
              <div class="support" @click="kudos(message.mid, index)">
                <i
                  v-if="message.mark == 1"
                  class="el-icon-thumb"
                  style="color: #007acc; font-weight: 700; font-size: 15px"
                  size="medium"
                ></i>
                <i v-else class="el-icon-thumb" size="medium"></i>
                <span
                  >点赞
                  <template v-if="!message.likes == 0">
                    ：{{ message.likes }}
                  </template></span
                >
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "searchMsg",
  data() {
    return {
      searchForm: {
        keyword: "",
      },
      msgsList: [],
      load: false,
      page: 1,
      size: 8,
      tip: "",
    };
  },
  computed: {
    ...mapState({
      userInfo: (state) => state.userInfo.userInfo,
    }),
  },
  methods: {
    goBack() {
      this.$router.push("/home");
    },

    async search() {
      this.load = true;
      let read = [];
      this.msgsList.forEach((x) => {
        read.push(x.mid);
      });

      let result = await this.$API.reqSearch(
        this.userInfo.uid,
        this.page,
        this.size,
        this.searchForm.keyword,
        read
      );

      if (result.code == 2000) {
        this.load = false;
        if (result.data.length == 0) {
          this.$message({
            message: "没有更多数据了哦",
            type: "info",
          });
          return;
        }
        this.msgsList = [
          ...this.msgsList,
          ...result.data.map((item) => {
            item.avatar = "http://cdn.gdxywsw.club/" + item.avatar;
            let newImages = [];
            for (let i of item.images) {
              i = "http://cdn.gdxywsw.club/" + i;
              newImages.push(i);
            }
            item.images = newImages;
            this.load = false;
            this.tip = "没有搜索到哦";
            return item;
          }),
        ];
      } else {
        this.load = false;
        return Promise.reject(new Error("fail"));
      }
      this.load = false;
    },
    // 查看发帖人信息
    getOtherUser(username, uid) {
      if (this.userInfo.nickname) {
        this.$router.push({ name: "myInfo", params: { uid } });
      } else {
        this.$message({
          message: "请先登录",
          type: "info",
        });
      }
    },

    // 删帖
    async deleteMsg(message, index) {
      let result = await this.$API.reqDeleteMessage(message.mid);
      if (result.code == 2000) {
        this.contentList.splice(index, 1);
      }
    },
    // 查看帖子详情
    toMessageDetail(message) {
      // const { userInfo } = this.userInfo;
      let userInfo = this.userInfo;

      this.$router.push({ name: "delMsg", params: { message, userInfo } });
    },

    // 点赞
    async kudos(mid, i) {
      if (this.contentList[i].mark == 0) {
        this.contentList = this.contentList.map((item, index) => {
          if (index == i) {
            item.mark = 1;
          }
          return item;
        });
        let result = await this.$API.reqKudos(mid);
        if (result.code === 2000) {
          console.log("11");
        }
      } else {
        this.contentList = this.contentList.map((item, index) => {
          if (index == i) {
            item.mark = 0;
          }
          return item;
        });
        let result = await this.$API.reqCancelKudos(mid);
        if (result.code === 2000) {
          console.log("22");
        }
      }
      this.search();
    },

    //上划加载
    handleScroll() {
      //变量scrollTop是滚动条滚动时，距离顶部的距离
      let scrollTop =
        document.documentElement.scrollTop || document.body.scrollTop;
      //变量windowHeight是可视区的高度
      let windowHeight =
        document.documentElement.clientHeight || document.body.clientHeight;
      //变量scrollHeight是滚动条的总高度
      let scrollHeight =
        document.documentElement.scrollHeight || document.body.scrollHeight;
      if (scrollTop + windowHeight >= scrollHeight - 15) {
        console.log("触底");
        if (this.load) {
          return;
        }

        this.page = this.page + 1;
        this.search(this.page);
      }
    },
  },
  mounted() {
    window.addEventListener("scroll", this.handleScroll, true);
  },
  beforeDestroy() {
    window.removeEventListener("scroll", this.handleScroll);
  },
};
</script>

<style lang="less" scoped>
.header {
  margin-top: 10px;
}

.detail-content {
  margin-top: 10px;
  .detail {
    border: 1px solid #0078d4;
    margin-bottom: 5px;
    // .message-imgs {
    //   display: flex;
    //   justify-content: space-around;
    //   flex-wrap: wrap;
    //   .el-image {
    //     flex: 1;
    //   }
    // }
    .footer {
      display: flex;
      justify-content: space-around;
      align-items: center;
      font-size: 12px;
      font-weight: 400;
      div {
        margin: 3px 0;
        i {
          margin-right: 2px;
        }
      }
    }

    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 50px;
      margin-top: 5px;
      .header-left {
        .avatar {
          margin: 3px;
        }
        display: flex;
        align-items: center;
        .username_time {
          .time {
            font-size: 12px;
          }
        }
      }
      .header-right {
        margin-top: -3px;
        margin-right: 5px;
      }
    }
    .message-content {
      .message-imgs {
        padding: 3px;
      }
      h4 {
        margin: 5px;
        padding: 0;
      }
      p {
        margin: 5px;
        font-size: 14px;
      }
      .el-image {
        margin: 5px;
      }
    }
  }
}
</style>